package com.dozen.share;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dozen.commonbase.router.ARouterLocation;
import com.dozen.commonbase.utils.StyleToastUtil;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {

    private TextView tvShareData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showThis();

        ShareConstant.shareBeanData = new ArrayList<>();

        tvShareData = findViewById(R.id.show_share_data);

        findViewById(R.id.main_clear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareConstant.shareBeanData = new ArrayList<>();
                index = -1;
            }
        });
        findViewById(R.id.main_test).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadNetShare3();
            }
        });
        findViewById(R.id.main_share_show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showData();
            }
        });
        findViewById(R.id.main_share_time).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadSharesData();
            }
        });


        showUrl();
        shareInit();
    }

    private static boolean showOne = false;
    private FrameLayout flShow;

    @SuppressLint("ClickableViewAccessibility")
    private void showThis() {
        flShow = findViewById(R.id.main_show);
        findViewById(R.id.main_show_top).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showOne = true;
            }
        });
        findViewById(R.id.main_show_bottom).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (showOne) {
                    flShow.setVisibility(View.GONE);
                }
            }
        });
        flShow.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
    }

    private int index = -1;
    private boolean run = false;

    private void loadNetShare3() {
        index++;
        if (index == ShareConstant.loadShareUrl().size()) {
            StyleToastUtil.info("结束");
            run = false;
            return;
        }
        run = true;
        ARouter.getInstance().build(ARouterLocation.app_shares).withSerializable("urlBean", ShareConstant.loadShareUrl().get(index)).navigation();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (run) {
            loadNetShare3();
        }
    }

    private SharesAdapter sharesAdapter;
    private RecyclerView recyclerView;
    private List<ShareBean> shareBeanList;

    private void shareInit() {
        recyclerView = findViewById(R.id.main_recycler_view_time);
        shareBeanList = new ArrayList<>();
        sharesAdapter = new SharesAdapter(shareBeanList);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        recyclerView.setAdapter(sharesAdapter);
    }

    @SuppressLint("NotifyDataSetChanged")
    private void loadSharesData() {
        List<ShareBean> list = DataSupport.order("id desc").find(ShareBean.class);

        List<ShareBean> managerList = new ArrayList<>();
        ShareBean shareBean = new ShareBean();
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getTimes().equals(shareBean.getTimes())) {
                shareBean.setName(shareBean.getName() + "_" + list.get(i).getName());
            } else {
                shareBean = list.get(i);
                managerList.add(shareBean);
            }
        }

        sharesAdapter.setList(managerList);
        sharesAdapter.notifyDataSetChanged();
    }

    private UrlAdapter urlAdapter;
    private RecyclerView urlRecyclerView;
    private List<UrlBean> urlBeanList;

    private void showUrl() {
        urlRecyclerView = findViewById(R.id.main_recycler_view);
        urlBeanList = ShareConstant.loadShareUrl();
        urlAdapter = new UrlAdapter(urlBeanList);

        //设置LayoutManager
        FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(this);
        //主轴为水平方向，起点在左端
        flexboxLayoutManager.setFlexDirection(FlexDirection.ROW);
        //按正常方向换行
        flexboxLayoutManager.setFlexWrap(FlexWrap.WRAP);
//        //定义项目在副轴轴上如何对齐
        flexboxLayoutManager.setAlignItems(AlignItems.CENTER);
//        //多个轴对齐方式
        flexboxLayoutManager.setJustifyContent(JustifyContent.FLEX_START);

        urlRecyclerView.setAdapter(urlAdapter);
        urlRecyclerView.setLayoutManager(flexboxLayoutManager);

        urlAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                UrlBean urlBean = urlAdapter.getData().get(position);
                urlBean.setShow(false);
                ARouter.getInstance().build(ARouterLocation.app_shares).withSerializable("urlBean", urlBean).navigation();
            }
        });

    }

    private void showData() {
        StringBuilder sb = new StringBuilder();
        List<ShareBean> list = ShareConstant.shareBeanData;

        StyleToastUtil.success("size:" + list.size());

        Collections.sort(list, new Comparator<ShareBean>() {
            @Override
            public int compare(ShareBean o1, ShareBean o2) {

                int i = 0;
                if (o1.getNumber() > o2.getNumber()) {
                    i = -1;
                } else if (o1.getNumber() < o2.getNumber()) {
                    i = 1;
                }

                return i;
            }
        });

        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i).getName()).append(":").append(list.get(i).getNumber()).append("\n");
        }

        tvShareData.setText(sb);

    }

}